package com.liyuanjinglyj.javacarddemo.adapter;

import com.liyuanjinglyj.javacarddemo.ResourceTable;
import com.liyuanjinglyj.javacarddemo.model.InfoItem;
import com.liyuanjinglyj.javacarddemo.slice.WebViewAbilitySlice;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import org.devio.hi.json.HiJson;

import java.util.ArrayList;
import java.util.List;

public class MyPageProvider extends PageSliderProvider {
    HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
    private AbilitySlice abilitySlice;
    private List<String> stringList;

    public MyPageProvider(List<String> list,AbilitySlice abilitySlice){
        this.stringList=list;
        this.abilitySlice=abilitySlice;
    }

    @Override
    public int getCount() {
        return this.stringList.size();
    }

    @Override
    public Object createPageInContainer(ComponentContainer componentContainer, int i) {
        final String url = this.stringList.get(i);
        ListContainer listContainer=new ListContainer(null);
        listContainer.setLayoutConfig(
                new StackLayout.LayoutConfig(
                        ComponentContainer.LayoutConfig.MATCH_PARENT,
                        ComponentContainer.LayoutConfig.MATCH_PARENT
                ));
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
            @Override
            public void onFailure(int code, String errorMessage) {
                //http访问出错了，此部分内容在主线程中工作;
                //可以更新UI等操作,请不要执行阻塞操作。
            }
            @Override
            public void onResponse(String response) {
                //http访问成功，此部分内容在主线程中工作;
                //可以更新UI等操作，但请不要执行阻塞操作。
                HiJson hiJson = new HiJson(response);
                int counts=hiJson.get("news_item").count();
                List<InfoItem> infoItemList=new ArrayList<>();
                for(int i=0;i<counts;i++){
                    InfoItem infoItem=new InfoItem();
                    infoItem.setTitle(hiJson.get("news_item").get(i).value("title"));
                    infoItem.setDigest(hiJson.get("news_item").get(i).value("digest"));
                    infoItem.setUrl(hiJson.get("news_item").get(i).value("url"));
                    infoItem.setThumb_url(hiJson.get("news_item").get(i).value("thumb_url"));
                    infoItemList.add(infoItem);
                }
                InfoItemListProvider infoItemListProvider=new InfoItemListProvider(infoItemList, abilitySlice);
                listContainer.setItemProvider(infoItemListProvider);
                listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
                    @Override
                    public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                        Intent intent=new Intent();
                        intent.setParam("url",infoItemList.get(i).getUrl());
                        abilitySlice.present(new WebViewAbilitySlice(),intent);
                    }
                });
            }
        });
        componentContainer.addComponent(listContainer);
        return listContainer;
    }

    @Override
    public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
        componentContainer.removeComponent((Component) o);
    }

    @Override
    public boolean isPageMatchToObject(Component component, Object o) {
        return true;
    }
}
